// pages/check/check.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    ready: false,
    active: 0,
    search: {
      pageNumber: 1,
      pageSize: 10,
      totalPages: 0
    },
    tabNum: {
      membershipNumber: 0,
      activityNumber: 0,
      taskNumber: 0
    },
    userList: [],
    activityList: [],
    resourceList: [],
    ismodal:false,
  },
  onChange(e) {
    console.log(e)
    this.toggle("active", e.currentTarget.dataset.index);
    console.log(this.data.active)
    this.refreshList();
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  check: function() {
    this.toggle("show", true);
  },
  onClose: function() {
    this.toggle("show", false);
  },
  contact: function(e) {
    console.log(e);
  },
  toPage(e) {
    if (this.data.isOrgCheck){
      return
    }
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn + "?";
    if (dataset.id) {
      url += "&id=" + dataset.id;
    }
    if (dataset.orgId) {
      url += "&orgId=" + dataset.orgId;
    }
    if (dataset.userId) {
      url += "&userId=" + dataset.userId;
    }
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  refreshList: function() {
    var that = this;
    that.toggle("req", false);
    app.getUserInfo(function(userInfo) {
      that.toggle("search.pageNumber", 1);
      that.toggle("search.totalPages", 0);
      that.list();
    });
  },
  list: function(append) {
    var that = this;
    var active = parseInt(that.data.active);
    console.log('接口', active)
    if (this.data.isOrgCheck == 1){
      that.resourceList();
    }else{
     
      switch (active) {
        case 0:
          that.userList(append);
          break;
        case 1:
          that.activityList(append);
          break;
        case 2:
          that.resourceList(append);
          break;
      }
    }
  },
  userList: function(append) {
    var that = this;
    app.req({
      url: "orgmini/manager/audit/getOrgMembership",
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          var nowData = res.data;
          var oriList = that.data.userList;
          var nowList = nowData.userList.rows;
          that.toggle("tabNum.membershipNumber", nowData.membershipNumber || 0);
          that.toggle("tabNum.activityNumber", nowData.activityNumber || 0);
          that.toggle("tabNum.taskNumber", nowData.taskNumber || 0);
          that.toggle("search.totalPages", nowData.userList.totalPages);
          that.toggle("ready", true);
          that.toggle("req", true);
          var year = new Date().getFullYear();
          var month = new Date().getMonth() + 1;
          var day = new Date().getDate();
          for (var item of nowList){
            // if (item.applyTime){
            //   if (year == item.applyTime.substring(0, 4)) {
            //     if (month == item.applyTime.substring(5, 7) && day == item.applyTime.substring(8, 10)) {
            //       item.applyTime = item.applyTime.substring(11, 16);
            //     } else {
            //       item.applyTime = item.applyTime.substring(5, 7) + '月' + item.applyTime.substring(8, 10) + '日 ' + item.applyTime.substring(11, 16);
            //     }
            //   } else {
            //     item.applyTime = item.applyTime.substring(0, 4) + '年' + item.applyTime.substring(5, 7) + '月' + item.applyTime.substring(8, 10) + '日 ' + item.applyTime.substring(11, 16);
            //   }
            // }
            for (var list of item.fieldContents){
              if (list.fieldName == "生日"){
                list.fieldContent = list.fieldContent.split(" ")[0];
              }
            }
          }
          that.toggle("userList", append ? oriList.concat(nowList) : nowList);
        }
      }
    });
  },
  activityList: function(append) {
    var that = this;
    console.log('接口请求')
    app.req({
      url: "orgmini/manager/audit/getActivityOrgList",
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          var nowData = res.data;
          var oriList = that.data.activityList;
          var nowList = nowData.actList.rows;
          that.toggle("tabNum.membershipNumber", nowData.membershipNumber || 0);
          that.toggle("tabNum.activityNumber", nowData.activityNumber || 0);
          that.toggle("tabNum.taskNumber", nowData.taskNumber || 0);
          that.toggle("search.totalPages", nowData.actList.totalPages);
          that.toggle("ready", true);
          that.toggle("req", true);
          var year = new Date().getFullYear();
          var month = new Date().getMonth() + 1;
          var day = new Date().getDate();
          for (var item of nowList) {
            if (item.createTime){
              if (year == item.createTime.substring(0, 4)) {
                if (month == item.createTime.substring(5, 7) && day == item.createTime.substring(8, 10)) {
                  item.createTime = item.createTime.substring(11, 16);
                } else {
                  item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
                }
              } else {
                item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
              }
            }
          }
          that.toggle("activityList", append ? oriList.concat(nowList) : nowList);
        }
      }
    });
  },
  resourceList: function(append) {
    var that = this;
    if (that.data.isOrgCheck == 1) {
      that.data.search.status = 0;
      this.toggle("search", this.data.search);
    }
    app.req({
      url: that.data.isOrgCheck == 1 ? "orgmini/orgAccount/getOrgAccountCashApplyList":"orgmini/org/newGetTaskAuditList",
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          var year = new Date().getFullYear();
          var month = new Date().getMonth() + 1;
          var day = new Date().getDate();
          that.toggle("req", true);
          if (that.data.isOrgCheck){
            var nowList = [];
            for (var i in res.data.rows){
              if (res.data.rows[i].status == 0){
                nowList.push(res.data.rows[i])
              }
            }
            for (var item of nowList) {
              if (item.applyTime) {
                if (year == item.applyTime.substring(0, 4)) {
                  if (month == item.applyTime.substring(5, 7) && day == item.applyTime.substring(8, 10)) {
                    item.applyTime = item.applyTime.substring(11, 16);
                  } else {
                    item.applyTime = item.applyTime.substring(5, 7) + '月' + item.applyTime.substring(8, 10) + '日 ' + item.applyTime.substring(11, 16);
                  }
                } else {
                  item.applyTime = item.applyTime.substring(0, 4) + '年' + item.applyTime.substring(5, 7) + '月' + item.applyTime.substring(8, 10) + '日 ' + item.applyTime.substring(11, 16);
                }
              }
            }
            that.toggle("resourceList",nowList);
          }else{
            var nowData = res.data;
            var oriList = that.data.resourceList;
            var nowList = nowData.userList.rows;
            that.toggle("tabNum.membershipNumber", nowData.membershipNumber || 0);
            that.toggle("tabNum.activityNumber", nowData.activityNumber || 0);
            that.toggle("tabNum.taskNumber", nowData.taskNumber || 0);
            that.toggle("search.totalPages", nowData.userList.totalPages);
            that.toggle("ready", true);
            for (var item of nowList) {
              if (item.createTime){
                if (year == item.createTime.substring(0, 4)) {
                  if (month == item.createTime.substring(5, 7) && day == item.createTime.substring(8, 10)) {
                    item.createTime = item.createTime.substring(11, 16);
                  } else {
                    item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
                  }
                } else {
                  item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16);
                }
              }
            }
            that.toggle("resourceList", append ? oriList.concat(nowList) : nowList);
          }
        }
      }
    });
  },
  // 打开文档
  openFile(e) {
    var dataset = e.currentTarget.dataset;
    console.log(dataset.url);
    wx.downloadFile({
      url: dataset.url,
      success: function (res) {
        console.log(res);
        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }
    })
  },
  dealApply(e) {
    if (this.data.isOrgCheck != 1){
      return
    }
    var that = this;
    var id = e.currentTarget.dataset.id;
    this.setData({
      ismodal: true,
      thisId: id
    })
    
  },
  //处理审核
  deal(e) {
    console.log("处理", this.data.thisId)
    if (e.currentTarget.dataset.type == "1") {
      this.doPass("1", this.data.thisId)
    } else {
      this.doPass("2", this.data.thisId)
    }
    this.setData({
      ismodal: false
    })
  },
  doPass(type, id) {
    var that = this;
    app.req({
      url: "orgmini/orgAccount/checkOrgAccountInitiation",
      method: "POST",
      data: {
        orgId: that.data.search.orgId,
        id: id,
        status:type
      },
      success: function (res) {
        if (res.code == 200) {
          that.refreshList();
        }
      },
    })
  },

  preventTouchMove() {},
  hide() {
    this.setData({
      ismodal: false
    })
  },
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgArr = [];
    for (var i = 0; i < this.data.userList.length; i++) {
      for (var j in this.data.userList[i].fieldContents){
        if (this.data.userList[i].fieldContents[j].dataType == 1){
          for (var k in this.data.userList[i].fieldContents[j].files){
            imgArr.push(this.data.userList[i].fieldContents[j].files[k].fileUrl);
          }
        }
      }
    };
    console.log(imgArr);
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.isOrgCheck){
      this.toggle("isOrgCheck", options.isOrgCheck);
    }
    if (options.id) {
      this.toggle("search.orgId", options.id);
    }
    if (options.orgId) {
      this.toggle("search.orgId", options.orgId);
    }
    if (options.sendType) {
      // this.toggle("sendType", options.sendType);
      if (options.sendType ==1){
        this.toggle("active",0);
      } else if (options.sendType == 2){
        this.toggle("active", 1);
      } else if (options.sendType == 3){
        this.toggle("active", 2);
      }
    }
    // this.toggle("isOrgCheck", "1");
    if (this.data.isOrgCheck == 1){
      wx.setNavigationBarTitle({
        title: '提现审核'
      })
    }else{
      wx.setNavigationBarTitle({
        title: options.orgname
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var search = this.data.search;
    if (search.pageNumber * 1 < search.totalPages * 1) {
      this.toggle("search.pageNumber", search.pageNumber * 1 + 1)
      this.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})